import {onMounted, ref} from 'vue';
import { useTableData } from "../../hooks/table/useTableData.js";
import { useConfirmAction } from "../../hooks/comfirm/useConfirmAction.js"
import {packageMgtDel,packageMgtChangeStatus} from "../../api/package.js";
import { ElMessage } from "element-plus";
export function usePackageTablaData() {
  const columns = ref([
    {
      label: '套餐名称',
      prop: 'name',
      align: 'center',
    },
    {
      label: '图片',
      isSlot: true,
      prop: 'image',
      align: 'center',
    },
    {
      label: '套餐分类',
      prop: 'categoryName',
      align: 'center',
    },
    {
      label: '套餐价格',
      prop: 'price',
      align: 'center',
    },
    {
      label: '售卖状态',
      prop: 'status',
      align: 'center',
      isSlot: true,
    },
    {
      label: '最后操作时间',
      prop: 'updateTime',
      align: 'center',
      isSlot: true,
    },
    {
      label: '操作',
      prop: 'general',
      align: 'center',
      isSlot: true,
      width: 200,
    },
  ]);
  const { confirm } = useConfirmAction()
  const selectedRows = ref([])
  function handleSelectionChange (rows) {
    selectedRows.value = rows.map((item) => item.id)
  }
  function removeMore() {
    if (selectedRows.value.length === 0) {
       ElMessage.warning('请选择要删除的数据！')
      return false
    }
    confirm('是否选择删除所选数据？', async () => {
      await packageMgtDel({ ids: selectedRows.value?.join(',') })
      await getTableData()
      selectedRows.value.splice(0)
    }, () => {
      return false
    })
  }
  async function changeStatusMore(type) {
    if (selectedRows.value.length === 0) {
      ElMessage.warning('请选择要操作的数据！')
      return false
    }
    await packageMgtChangeStatus({ ids: selectedRows.value?.join(','), status: type })
    await getTableData()
  }
  async function changeStatus(row) {
   await packageMgtChangeStatus({ ids: row.id, status: row.status === 1 ? 0 : 1 })
    await getTableData()
  }
  function removeOne(row) {
    confirm('是否要删除当前数据？', async () => {
      await packageMgtDel({ ids: row.id })
      await getTableData()
    }, () => {
      return false
    })
  }
  const { tableData, getTableData,total, prevClick, nextClick, sizeChange, currentChange,loading } = useTableData('/setmeal/query')
  onMounted(() => {
    getTableData().then(r => {})
  })
  return {
    columns,
    tableData,
    removeMore,
    changeStatusMore,
    changeStatus,
    removeOne,
    getTableData,
    total,
    prevClick,
    nextClick,
    sizeChange,
    currentChange,
    handleSelectionChange,
    loading
  };
}
